package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.InputConstants;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The type debateStructure specifies the overall content model of the document types that describe debates.
 * 
 * <pre>
 * 	<xsd:complexType name="debateStructure">
 * 		<xsd:sequence>
 * 			<xsd:element ref="meta"/>
 * 			<xsd:element ref="coverPage" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="preface" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="debateBody"/>
 * 			<xsd:element ref="conclusions" minOccurs="0" maxOccurs="1"/>
 * 			<xsd:element ref="attachments" minOccurs="0" maxOccurs="1"/>
 * 		</xsd:sequence>
 * 		<xsd:attribute name="contains" type="versionType" default="originalVersion"/>
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public abstract class DebateStructure implements Externalizable {

	private final Meta _meta = new Meta();
	
	private CoverPage _coverPage;
	private Preface _preface;
	private Conclusions _conclusions;
	private Attachments _attachments;
	
	private final DebateBody _debateBody = new DebateBody();

	public Meta getMeta() {
		return _meta;
	}

	public CoverPage getCoverPage() {
		return _coverPage;
	}

	public void setCoverPage(CoverPage coverPage) {
		_coverPage = coverPage;
	}

	public Preface getPreface() {
		return _preface;
	}

	public void setPreface(Preface preface) {
		_preface = preface;
	}

	public DebateBody getDebateBody() {
		return _debateBody;
	}

	public Conclusions getConclusions() {
		return _conclusions;
	}

	public void setConclusions(Conclusions conclusions) {
		_conclusions = conclusions;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		_meta.writeExternal(out);
		
		if (_coverPage != null) {
			_coverPage.writeExternal(out);
		}
		
		if (_preface != null) {
			_preface.writeExternal(out);
		}
		
		_debateBody.writeExternal(out);
		
		if (_conclusions != null) {
			_conclusions.writeExternal(out);
		}
		
		if (_attachments != null) {
			_attachments.writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		in.readNextStartElement();
		_meta.readExternal(in);

		if (CoverPage.ELEMENT == in.getLocalName()) {
			_coverPage = new CoverPage();
			_coverPage.readExternal(in);
		}

		if (InputConstants.END_ELEMENT == in.getEventType()) {
			in.readNextStartElement();
		}

		if (Preface.ELEMENT == in.getLocalName()) {
			_preface = new Preface();
			_preface.readExternal(in);
		}

		if (InputConstants.END_ELEMENT == in.getEventType()) {
			in.readNextStartElement();
		}
		
		_debateBody.readExternal(in);
		
		if (InputConstants.END_ELEMENT == in.getEventType()) {
			in.readNextStartElement();
		}
		
		if (in.getEventType() == InputConstants.START_ELEMENT && "conclusions".equals(in.getLocalName())) {
			_conclusions = new Conclusions();
			_conclusions.readExternal(in);
		}
		
	}

	protected final void internalAccept(AKVisitor visitor) {
	}

}